<template>
  <div class="home">
    <tab @click="tabClick"></tab>
    <div class="order_containers" :class="boxClass">
      <div class="order_item list">
        <div class="all_set">
          <span><i class="iconfont icon017check03" :class="allClass" @click="choseAll"></i>全选</span>
          <span style="color: olivedrab;" @click="del">删除选中</span>
        </div>
        <ul>
          <li v-for="(item, i) in $store.state.orders" :key="item.id">
              <div @click="checkOrder(item, i)">
                  <section>
                      <h3>{{item.name}}</h3>
                      <span>订单价格：{{item.price}}</span>
                  </section>
                  <section class="price">
                      <p>商品描述：{{item.subscribe}}</p>
                      <span class="iconfont icon017check03" :class="item.check ? 'active' : ''"></span>
                      <!-- <span @click="changeState(item.state, i)">{{item.state | typeText}}</span> -->
                  </section>
              </div>
          </li>
        </ul>
      </div>
      <list :state="0" class="order_item"></list>
      <list :state="1" class="order_item"></list>
      <list :state="2" class="order_item"></list>
      <list :state="3" class="order_item"></list>
    </div>
  </div>
</template>

<script>
import tab from '@/components/tab/tab'
import list from '@/components/list/list'
import axios from 'axios'
export default {
  name: 'Home',
  components: {
    tab,
    list
  },
  filters: {
    typeText(val) {
      let text = ''
      switch(val) {
        case 0:
        text = '付款'
        break
        case 1:
        text = '发货'
        break
        case 2:
        text = '收货'
        break
        case 3:
        text = '再来一单'
        break
      }
      return text
    }
  },
  data() {
    return {
      boxClass: 'classFirst',
      allClass: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      axios.get('/data.json').then(res => {
        if(this.$store.state.orders.length === 0) {
          this.$store.commit('changeOrders', res.data.goods)
        }
      })
    },
    tabClick(i) {
      switch(i) {
        case 0:
          this.boxClass = 'classFirst'
        break;
        case 1:
          this.boxClass = 'classSecond'
        break;
        case 2:
          this.boxClass = 'classThird'
        break;
        case 3:
          this.boxClass = 'classFouth'
        break;
        case 4:
          this.boxClass = 'classFifth'
        break;
      }
    },

    checkOrder(item, i) {
      item.check = !item.check
      // var check = !item.check
      // this.$store.commit('changeCheck', {i, check})
      var flag = false
      this.$store.state.orders.forEach(item => {
        if(!item.check) {
          flag = true
        }
      });
      
      if(flag) {
        this.allClass = ''
      } else {
        this.allClass = 'active'
      }
    },

    choseAll() {
      this.allClass = this.allClass === '' ? 'active' : ''
      if(this.allClass == 'active') {
        this.$store.state.orders.forEach(item => {
          item.check = true
        });
      } else {
        this.$store.state.orders.forEach(item => {
          item.check = false
        });
      }
    },

    del() {
      let arr = this.$store.state.orders.filter(item => {
        return !item.check
      })
      if(this.$store.state.orders.length == 0) {
        this.allClass = ''
      }
      this.$store.commit('changeOrders', arr)
    }
  }
}
</script>
<style lang="scss" scoped>
  .home {
    height: 100%;
    overflow: hidden;
  }
  .order_containers {
    height: calc(100% - 60px);
    overflow: auto;
    display: flex;
    width: 500%;
    background-color: #f6f6f6;
    position: relative;
    transition: all .5s linear;
    .order_item {
      width: 20%;
    }
    .all_set {
      display: flex;
      justify-content: space-between;
      height: 40px;
      padding: 0 15px;
      align-items: center;
    }
  }
  ul li div {
      background-color: #fff;
      padding: 10px;
      margin-top: 12px;
  }
  .price {
      display: flex;
      justify-content: space-between;
      align-items: center;
      p {
          width: 200px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
      }
      span {
          padding: 6px 15px;
          background-color: olivedrab;
          border-radius: 6px;
          color: #fff;
          font-size: 12px;
      }
      .icon017check03 {
        background-color: #fff;
        color: #333;
        font-size: 20px;
      }
  }
  .icon017check03 {
    font-size: 20px;
  }
  .icon017check03.active {
    color: olivedrab;
  }
  .classFirst {
    left: 0%;
  }
  .classSecond {
    left: -100%;
  }
  .classThird {
    left: -200%;
  }
  .classFouth {
    left: -300%;
  }
  .classFifth {
    left: -400%;
  }
</style>
